package com.gxc.datacore.query.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxc.authcore.system.cache.UserCache;
import com.gxc.authcore.system.util.UserInfo;
import com.gxc.datacore.metadata.domain.Column;
import com.gxc.datacore.metadata.domain.Table;
import com.gxc.datacore.query.service.DataAccessService;
import com.gxc.dbcore.dao.SqlDao;

@Service
public class DataAccessServiceImpl implements DataAccessService {

	@Autowired
	private SqlDao sqlDao;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<Table> getTableList(String user_id) {
		UserInfo userInfo = UserCache.get(user_id);
		if (userInfo != null) {
			Map params = new HashMap();
			params.put("user", userInfo.getUser());
			params.put("roleList", userInfo.getRoleList());
			return sqlDao.selectList("datacore.dataAccess.listUserTables",
					params);
		}
		return null;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<Column> getColumnList(String user_id, String table_id) {
		UserInfo userInfo = UserCache.get(user_id);
		if (userInfo != null && table_id != null) {
			Map params = new HashMap();
			params.put("user", userInfo.getUser());
			params.put("roleList", userInfo.getRoleList());
			params.put("table_id", table_id);
			return sqlDao.selectList("datacore.dataAccess.listUserColumns",
					params);
		}
		return null;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean checkAccess(String user_id, String table_id) {
		UserInfo userInfo = UserCache.get(user_id);
		if (userInfo != null && table_id != null) {
			Map params = new HashMap();
			params.put("user", userInfo.getUser());
			params.put("roleList", userInfo.getRoleList());
			params.put("table_id", table_id);
			List list = sqlDao.selectList("datacore.dataAccess.checkUserTable",
					params);
			if (list != null && list.size() > 0) {
				return true;
			}
		}
		return false;
	}

}
